<?php

/**
 * commandeClient actions.
 *
 * @package    ProjetBar
 * @subpackage commandeClient
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class commandeClientActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
    $this->commande_clients = Doctrine_Core::getTable('commandeClient')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
  	$this->id_commande_client = $request->getParameter('id_commande_client');
    $this->commande_client = Doctrine_Core::getTable('commandeClient')->find(array($request->getParameter('id_commande_client')));
    #$this->forward404Unless($this->commande_client);
	
	$requete_mauvaise_requete = Doctrine_Query::Create()
		->select('libelle_produit')
		->from('ProduitEnVente');
	$this->mauvaise_requete = $requete_mauvaise_requete->fetchArray();
	#print_r($this->mauvaise_requete);
	
	$this->idCommandeClient = $request->getParameter('id_commande_client');
	$this->idTypePaiement = $request->getParameter('id_type_paiement');
	$this->libelleTypePaiement = Doctrine_Core::getTable('typePaiement')->find($this->idTypePaiement);
	$this->idTableClient = $request->getParameter('id_table_client');
	$requete_menus_commande_client = Doctrine_Query::Create()
		->select('pc.id_produit as id_produit, pv.prix_unitaire_produit_en_vente, pc.quantite_produit, pv.libelle_produit, c.libelle_categorie,
		pc.id_etat_produit_en_vente as id_etat_produit_en_vente')
		->from('produitEnVente pv')
		->innerJoin('pv.Produit p')
		->innerJoin('pv.ProduitEnCommandeALaCarte pc')
		->innerJoin('pv.Categorie c')
		->where('pc.id_commande_client = '.$this->idCommandeClient.'')
		->orderBy('c.libelle_categorie');
	$this->menus_commande_client = $requete_menus_commande_client->fetchArray();
	
	$this->produitMenus = Doctrine_Query::Create()
		->select('cm.id_commande_menu as id_commande_menu, m.id_menu as id_menu, m.libelle_menu, m.prix_unitaire_menu,pcm.id_produit as id_produit, 
		pv.libelle_produit as libelle_produit, pcm.id_etat_produit_en_vente as id_etat_produit_en_vente')
		->from('commandeMenu cm')		
		->innerJoin('cm.Menu m')
		->innerJoin('cm.ProduitEnCommandeViaMenu pcm')
		->innerJoin('cm.CommandeClient cc')
		->innerJoin('pcm.ProduitEnVente pv')		
		->where('cc.id_commande_client = :id_commande_client', array(':id_commande_client' => $this->idCommandeClient))
		->fetchArray();
  }

  public function executeNew(sfWebRequest $request)
  {
    //$this->form = new commandeClientForm();
	$commandeClient = new commandeClient();
	$commandeClient->id_serveur = $_SESSION['id_serveur'];
	$commandeClient->id_table_client = $request->getParameter('id_table_client');
	$commandeClient->id_etat_commande = '1';
	$commandeClient->date_commande_client = date('c');
	$commandeClient->save();
	$this->redirect('commandeClient/commandesParTable?id_table_client='.$request->getParameter('id_table_client').'');
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new commandeClientForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($commande_client = Doctrine_Core::getTable('commandeClient')->find(array($request->getParameter('id_commande_client'))), sprintf('Object commande_client does not exist (%s).', $request->getParameter('id_commande_client')));
    $this->form = new commandeClientForm($commande_client);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($commande_client = Doctrine_Core::getTable('commandeClient')->find(array($request->getParameter('id_commande_client'))), sprintf('Object commande_client does not exist (%s).', $request->getParameter('id_commande_client')));
    $this->form = new commandeClientForm($commande_client);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();
	$produit_commande = Doctrine_Query::create()
		->delete()
		->from('produitEnCommandeALaCarte p')
		->where('p.id_commande_client = '.$request->getParameter('id_commande_client').'')
		->execute();

    $this->forward404Unless($commande_client = Doctrine_Core::getTable('commandeClient')->find(array($request->getParameter('id_commande_client'))), sprintf('Object commande_client does not exist (%s).', $request->getParameter('id_commande_client')));
    $commande_client->delete();

    $this->redirect('commandeClient/commandesParTable?id_table_client='.$request->getParameter('id_table_client').'');
  }
  
  public function executeCommandesParTable(sfWebRequest $request)
  {
	$this->id_table_client = $request->getParameter('id_table_client');
	$this->commandes_par_table = array();
	$this->commandes_par_table = Doctrine_Core::getTable('commandeClient')
		->createQuery('a')
		->where('a.id_table_client = '.$this->id_commande_client.'')
		->andWhere('a.id_serveur = '.$_SESSION['id_serveur'].'')
		->andWhere('a.id_etat_commande <> 2')
		->execute();	
	$requete_commande_notification_cartes = Doctrine_Query::Create()
		->select('c.id_commande_client, count(pc.id_produit) as nb_produit_pret')
		->from('commandeClient c, produitEnCommandeALaCarte pc')
		->where('pc.id_etat_produit_en_vente = 2')
		->andWhere('pc.id_commande_client = c.id_commande_client')
		->andWhere('c.id_table_client = :id_table_client', array(':id_table_client' => $this->id_table_client))
		->andWhere('c.id_serveur = :id_serveur', array(':id_serveur' => $_SESSION['id_serveur']))
		->groupBy('c.id_commande_client');
	$this->commande_notification_cartes = $requete_commande_notification_cartes->fetchArray();
	$requete_commande_notification_menus = Doctrine_Query::Create()
		->distinct($flag = true)
		->select('c.id_commande_client, count(pm.id_produit) as nb_produit_pret')
		->from('commandeClient c, produitEnCommandeViaMenu pm')
		->where('pm.id_etat_produit_en_vente = 2')
		->andWhere('pm.id_commande_client = c.id_commande_client')
		->andWhere('c.id_table_client = :id_table_client', array(':id_table_client' => $this->id_table_client))
		->andWhere('c.id_serveur = :id_serveur', array(':id_serveur' => $_SESSION['id_serveur']))
		->groupBy('c.id_commande_client');
	$this->commande_notification_menus = $requete_commande_notification_menus->fetchArray();
	}
	
	public function executeCommandePrete(sfWebRequest $request)
	{
		Doctrine_Query::Create()
			->update('commandeClient c')
			->set('c.id_etat_commande','2')
			->where('c.id_commande_client = '.$request->getParameter('id_commande_client'))
			->execute();
		 $this->redirect('commandeClient/commandesParTable?id_table_client='.$request->getParameter('id_table_client').'');
		
	}
	
	public function executeCategorieOuMenu(sfWebRequest $request)
	{
		$this->idCommandeClient = $request->getParameter('id_commande_client');
		$this->categories = Doctrine_Query::Create()
			->select('c.*')
			->from('categorie c')
			->orderBy('c.libelle_categorie')
			->fetchArray();
		$this->menus = Doctrine_Query::Create()
			->select('m.*')
			->from('menu m')
			->orderBy('m.libelle_menu')
			->fetchArray();
	}

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $commande_client = $form->save();

      $this->redirect('commandeClient/edit?id_commande_client='.$commande_client->getIdCommandeClient());
    }
  }
 }